package com.gimi.cloud.jimi.manage.service.prod.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.commonService.common.JmOperationLogService;
import com.gimi.cloud.jimi.dao.dao.prod.ProdBrandDao;
import com.gimi.cloud.jimi.dto.base.BaseDTO;
import com.gimi.cloud.jimi.dto.prod.ProdBrandDTO;
import com.gimi.cloud.jimi.entity.prod.ProdBrandEntity;
import com.gimi.cloud.jimi.dto.params.ProdBrandListParams;
import com.gimi.cloud.jimi.manage.service.prod.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    private final ProdBrandDao brandDao;
    private JmOperationLogService jmOperationLogService;

    @Autowired
    public BrandServiceImpl(ProdBrandDao brandDao,JmOperationLogService jmOperationLogService) {
        this.brandDao = brandDao;
        this.jmOperationLogService = jmOperationLogService;
    }


    /**
     * 添加商品品牌
     *
     * @param prodBrandDTO
     * @return
     */
    @Override
    public ResponseCodeEnum brandAdd(ProdBrandDTO prodBrandDTO) {
        //判断是否已经存在该名称的品牌，也就是说品牌名称不能重复
        List<ProdBrandDTO> isExist = brandDao.getBrandListByName(prodBrandDTO.getProdBrandName());
        if (isExist != null && isExist.size() > 0) {
            return ResponseCodeEnum.ADD_PRODBRAND_IS_EXIST;
        }
        prodBrandDTO.setProdBrandLogo(AliOSSUtil.getFilePath(prodBrandDTO.getProdBrandLogo()));
        ProdBrandEntity brandEntity = new ProdBrandEntity();
        BeanUtils.copyProperties(prodBrandDTO, brandEntity);
        brandDao.insert(brandEntity);
        //------------2018/2/2删除，不填加到中间表，改为添加三级品类的时候添加------------------------
//            CategoryBrandRelaEntity categoryBrandRelaEntity = new CategoryBrandRelaEntity();
//            categoryBrandRelaEntity.setProdBrandId(brandEntity.getPdId()+"");
//            categoryBrandRelaEntity.setProdCatId(prodBrandDTO.getProdCatId());
//            categoryBrandRelaDao.insert(categoryBrandRelaEntity);

        //添加操作日志
        BaseDTO baseDTO = new BaseDTO();
        BeanUtils.copyProperties(prodBrandDTO, baseDTO);
        jmOperationLogService.insertAddLog(baseDTO, brandEntity.getPdId() + "", "brandAdd", "ec_prod_brand", "添加商品品牌");
        return ResponseCodeEnum.SUCCESSED;
    }

    /**
     * 更新商品品牌
     *
     * @param prodBrandDTO
     * @return
     */
    @Override
    public ResponseCodeEnum brandUpdate(ProdBrandDTO prodBrandDTO) {
        //判断是否已经存在该名称的商品
        List<ProdBrandDTO> isExist = brandDao.getBrandListByName(prodBrandDTO.getProdBrandName());
        if (isExist != null && isExist.size() > 0 && prodBrandDTO.getPdId().intValue() != isExist.get(0).getPdId().intValue()) {
            return ResponseCodeEnum.ADD_PRODBRAND_IS_EXIST;
        }

        prodBrandDTO.setProdBrandLogo(AliOSSUtil.getFilePath(prodBrandDTO.getProdBrandLogo()));
        ProdBrandEntity BrandEntity = new ProdBrandEntity();
        BeanUtils.copyProperties(prodBrandDTO, BrandEntity);
        ProdBrandEntity brandEntityTemp = brandDao.selectByPrimaryKey(prodBrandDTO.getPdId());
        //将原来的禁用或启用，重新赋值给对象
        BrandEntity.setVersion(brandEntityTemp.getVersion());
        BrandEntity.setType(brandEntityTemp.getType());
        brandDao.updateByPrimaryKey(BrandEntity);
        //添加操作日志
        BaseDTO baseDTO = new BaseDTO();
        BeanUtils.copyProperties(prodBrandDTO, baseDTO);
        jmOperationLogService.insertUpdateLog(baseDTO, "brandUpdate", "ec_prod_brand", prodBrandDTO.getPdId() + "", "更新商品品牌");

        //------------2018/2/2删除，不填加到中间表，改为修改三级品类的时候修改------------------------
//            CategoryBrandRelaEntity categoryBrandRelaEntity =categoryBrandRelaDao.findByBrandId(prodBrandDTO.getPdId());
//            if(categoryBrandRelaEntity!=null){
//                categoryBrandRelaEntity.setProdCatId(prodBrandDTO.getProdCatId());
//                categoryBrandRelaDao.updateByPrimaryKeySelective(categoryBrandRelaEntity);
//            }
        return ResponseCodeEnum.SUCCESSED;

    }

    /**
     * 通过主键id删除商品品牌
     *
     * @param prodBrandDTO
     * @return
     */
    @Override
    public Boolean brandDelete(ProdBrandDTO prodBrandDTO) {
        brandDao.brandDelete(prodBrandDTO);
        //添加操作日志
        BaseDTO baseDTO = new BaseDTO();
        BeanUtils.copyProperties(prodBrandDTO, baseDTO);
        jmOperationLogService.insertUpdateLog(baseDTO, "brandDelete", "ec_prod_brand", prodBrandDTO.getPdId() + "", "通过主键id删除商品品牌，逻辑删除/启用");

        return true;
    }

    @Override
    public List<ProdBrandDTO> getBrandListById(ProdBrandListParams params) {
        List<ProdBrandDTO> result = brandDao.getBrandListById(params.getPdId());
        if (result != null) {
            for (ProdBrandDTO pb : result) {
                pb.setProdBrandLogo(AliOSSUtil.getFileUrl(pb.getProdBrandLogo(), AliOSSUtil.getNoEffictDateByDay(1)));
            }
        }
        return result;
    }

    @Override
    public List<ProdBrandDTO> getBrandList() {
        List<ProdBrandDTO> result = brandDao.getBrandList();
        if (result != null) {
            for (ProdBrandDTO pb : result) {
                pb.setProdBrandLogo(AliOSSUtil.getFileUrl(pb.getProdBrandLogo(), AliOSSUtil.getNoEffictDateByDay(1)));
            }
        }
        return result;
    }

    @Override
    public ProdBrandDTO gotoEditProdBrand(ProdBrandDTO prodBrandDTO) {
        ProdBrandDTO newprodBrandDTO = brandDao.findProdBrandByPdId(prodBrandDTO);
        newprodBrandDTO.setProdBrandLogo(AliOSSUtil.getFileUrl(newprodBrandDTO.getProdBrandLogo(), AliOSSUtil.getNoEffictDateByDay(1)));
        //添加操作日志
        jmOperationLogService.insertQueryLog(new BaseDTO(), "gotoEditProdBrand", "ec_prod_brand",
                prodBrandDTO.getPdId() + "", "转跳到编辑界面");
        return newprodBrandDTO;
    }

    @Override
    public Page<ProdBrandDTO> getBrandPageList(ProdBrandDTO prodBrandDTO) {
        PageHelper.startPage(prodBrandDTO.getCurrentPage(), prodBrandDTO.getPageSize());
        List<ProdBrandDTO> result = brandDao.queryPageList(prodBrandDTO);
        if (result != null) {
            for (ProdBrandDTO pb : result) {
                pb.setProdBrandLogo(AliOSSUtil.getFileUrl(pb.getProdBrandLogo(), AliOSSUtil.getNoEffictDateByDay(1)));
            }
        }

        Page<ProdBrandDTO> page = new Page(new PageInfo<>(result));
        return page;
    }
}
